import { isArray } from 'lodash-es'

/**
 * 记录
 * 1. 外层有group包裹时，checkbox内部会访问value.length属性，所以value必须是一个数组
 */
export default {
  functional: true,
  render(h, { data, props, listeners }) {
    const { options = [], value } = props
    // 绑定值为非数组时处理
    if (!isArray(value)) {
      listeners.input([])
      return
    }
    return (
      <el-checkbox-group {...data}>
        {options.map(options => (
          <checkboxs {...{ attrs: options }}></checkboxs>
        ))}
      </el-checkbox-group>
    )
  }
}

export const checkboxs = {
  functional: true,
  render(h, { data, props }) {
    const { label } = props
    return <el-checkbox {...data}>{label}</el-checkbox>
  }
}
